package com.bp.web.system;

import com.bp.common.annotation.Log;
import com.bp.common.core.controller.BaseController;
import com.bp.common.core.domain.AjaxResult;
import com.bp.common.enums.EnumBusinessType;
import com.bp.common.utils.poi.ExcelUtil;
import com.bp.system.domain.SysDept;
import com.bp.system.domain.SysRegion;
import com.bp.system.service.ISysRegionService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * 区域Controller
 *
 * @author shaominjie
 * @date 2020-08-21
 */
@RestController
@RequestMapping("/system/region")
public class SysRegionController extends BaseController {
    @Resource
    private ISysRegionService regionService;

    /**
     * 查询区域列表
     */
    @PreAuthorize("@ss.hasPermi('system:region:list')")
    @GetMapping("/list")
    public AjaxResult list(SysRegion sysRegion) {
        List<SysRegion> list = regionService.selectList(sysRegion);
        return AjaxResult.success(list);
    }

    /**
     * 导出区域列表
     */
    @PreAuthorize("@ss.hasPermi('system:region:export')")
    @Log(title = "区域", businessType = EnumBusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(SysRegion sysRegion) {
        List<SysRegion> list = regionService.selectList(sysRegion);
        ExcelUtil<SysRegion> util = new ExcelUtil<SysRegion>(SysRegion.class);
        return util.exportExcel(list, "region");
    }

    /**
     * 获取区域详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:region:query')")
    @GetMapping(value = "/{regionId}")
    public AjaxResult getInfo(@PathVariable("regionId") Long regionId) {
        return AjaxResult.success(regionService.selectById(regionId));
    }

    /**
     * 新增区域
     */
    @PreAuthorize("@ss.hasPermi('system:region:add')")
    @Log(title = "区域", businessType = EnumBusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody SysRegion sysRegion) {
        return toAjax(regionService.insert(sysRegion));
    }

    /**
     * 修改区域
     */
    @PreAuthorize("@ss.hasPermi('system:region:edit')")
    @Log(title = "区域", businessType = EnumBusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody SysRegion sysRegion) {
        return toAjax(regionService.update(sysRegion));
    }

    /**
     * 删除区域
     */
    @PreAuthorize("@ss.hasPermi('system:region:remove')")
    @Log(title = "区域", businessType = EnumBusinessType.DELETE)
    @DeleteMapping("/{regionIds}")
    public AjaxResult remove(@PathVariable Long[] regionIds) {
        return toAjax(regionService.deleteByIds(regionIds));
    }


    /**
     * 获取区域下拉树列表
     */
    @GetMapping("/treeSelect")
    public AjaxResult treeSelect(SysRegion region) {
        List<SysRegion> regions = regionService.selectList(region);
        return AjaxResult.success(regionService.buildRegionTreeSelect(regions));
    }
}
